#Kyle Kelly
#CS2110-B2
#Python disassembler

def disassemble(command):
        #command = command &65536
        opCode = command>>12
        output = ""

        if(opCode == 0b0001):           #ADD opCode
                output += "ADD "
                output += "R"+str((command>>9)&(0b111) ) + ", "# Destination registry added to string
                output += "R"+ str((command>>6)&(0b111)) # Pulling Source registry 1
                if((command>>5)&1): #for imm5
                        output += ", "+str((command&0b11111))
                else: #else SR2 registry
                        output += ", R" + str((command&0b111))             
                
        elif(opCode == 0b0101):           #AND opCode
                output += "AND "
                output += "R" +str((command>>9)&(0b111)) +", "# Destination registry added to string
                output += "R"+ str((command>>6)&(0b111)) # Pulling Source registry 1
                if((command>>5)&1): #for imm5
                        output += ", "+ str(hex((command&0b11111)))
                else: #else SR2 registry
                        output += ", R" + str(command&0b111)
                
        elif(opCode == 0b0000):           #BR opCode
                output += "BR"
                if((command>>11)&1):
                                output+="N"
                if((command>>10)&1):
                                output +="Z"
                if((command>>9)&1):
                        output +="P"
                output += " "+ str(hex(command&0b111111111))
                if(not (command>>9)):
                        return "NOP" #Chuck Testa!
			
        elif(opCode == 0b1100):           #JMP opCode
                if(((command>>6)&7)==7):
                        output = "RET"
                else:
                        output+= "JMP "
                        output += "R" + str((command>>6)&0b111)
                
        elif(opCode == 0b0100):           #JSR opCode
                output+="JSR"
                if((command>>11)&1):
                        output+= " "+ str(hex(command&0b11111111111))
                else:
                        output+="R R" + str((command>>6)&0b111)
                                        
        elif(opCode == 0b0010):           #LD opCode
                output += "LD "
                output += "R" + str((command>>9)&0b111) + " "
                output += str(hex(command&0b111111111))
                
        elif(opCode == 0b1010):           #LDI opCode
                output += "LDI "
                output += "R" + str((command>>9)&0b111) + " "
                output += str(hex(command&0b111111111))
                
        elif(opCode == 0b0110):           #LDR opCode
                output += "LDR "
                output += "R" + str((command>>9)&0b111) + " "
                output += "R" + str((command>>6)&0b111)
                output += " " + str(hex(command&0b111111))
                
        elif(opCode == 0b1110):           #LEA opCode
                output += "LEA "
                output += "R" + str((command>>9)&0b111) + " "
                output += str(hex(command&0b111111111))
                
        elif(opCode == 0b1001):           #NOT opCode
                output += "NOT "
                output += "R" + str((command>>9)&0b111) + " "
                output += "R" + str((command>>6)&0b111)

        elif(opCode == 0b0011):           #ST opCode
                output += "ST "
                output += "R" + str((command>>9)&0b111) + " "
                output += str(hex(command&0b111111111))
                
        elif(opCode == 0b1011):           #STI opCode
                output += "STI "
                output += "R" + str((command>>9)&0b111) + " "
                output += str(hex(command&0b111111111))
                
        elif(opCode == 0b0111):           #STR opCode
                output += "STR "
                output += "R" + str((command>>9)&0b111) + " "
                output += "R" + str((command>>6)&0b111)
                output += " " + str(hex(command&0b111111))
                
        elif(opCode == 0b1111):           #TRAP opCode
                if(command == 0xF025):
                        output += "HALT"
                else:
                        output = "ERROR"
                        
        else:
                output = "ERROR"

        return output


def main(listofinstruct):
        for x in listofinstruct:
                if(x == 0xFFFF):
                        return
                print disassemble(x)


theList=[0x1000, 0x1283, 0x1142, 0x5483, 0x5244, 0x526C, 0x0E7B, 0x0E00, 
0x040E, 0x0A03, 0xC1C0, 0xC080, 0x4821, 0x4040, 0x2411, 0xA60A, 0x6289, 
0xE20C, 0x937F, 0x360F, 0xB637, 0x74CA, 0x8000, 0xD000, 0xF025, 0xFFFF]

main(theList)
